package br.financeiro;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import br.financeiro.adapters.ItemEmailFormAdapter;
import br.financeiro.adapters.ItemPhoneFormAdapter;
import br.financeiro.dao.DBHelper;
import br.financeiro.entities.Mail;
import br.financeiro.entities.Person;
import br.financeiro.entities.Phone;
import br.financeiro.enums.TypeMail;
import br.financeiro.enums.TypePhone;
import br.financeiro.utils.AndroidUtils;
import br.financeiro.utils.Data;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;

public class CreatePerson extends OrmLiteBaseActivity<DBHelper> {

	private List<Mail> mails = new ArrayList<Mail>();
	private List<Phone> phones = new ArrayList<Phone>();

	private Dao<Person, Integer> personManager = null;
	private Dao<Mail, Integer> mailManager = null;
	private Dao<Phone, Integer> phoneManager = null;
	private ListView listMails;
	private ListView listPhones;

	@Override
	protected void onSaveInstanceState( Bundle outState ) {
		super.onSaveInstanceState( outState );
		Data dataMails = new Data( mails );
		outState.putSerializable( "mails", dataMails );
		Data dataPhones = new Data( phones );
		outState.putSerializable( "phones", dataPhones );
	}

	// Buttons Action
	private OnClickListener btnAddMailListener = new OnClickListener() {

		@Override
		public void onClick( View view ) {
			addMail();
			( (BaseAdapter) listMails.getAdapter() ).notifyDataSetChanged();
			AndroidUtils.setListViewHeightBasedOnChildren( listMails );
		}

	};

	private OnClickListener btnAddPhoneListener = new OnClickListener() {

		@Override
		public void onClick( View view ) {
			addPhone();
			( (BaseAdapter) listPhones.getAdapter() ).notifyDataSetChanged();
			AndroidUtils.setListViewHeightBasedOnChildren( listPhones );
		}

	};

	private OnClickListener btnSaveListener = new OnClickListener() {

		@Override
		public void onClick( View v ) {
			savePerson();
		}

	};

	@SuppressWarnings( "unchecked" )
	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.create_person_layout );
		try {
			personManager = getHelper().getPersonDao();
			mailManager = getHelper().getMailDao();
			phoneManager = getHelper().getPhoneDao();

		} catch ( SQLException e ) {
			Log.e( "ERRO", e.getMessage() );
			Toast.makeText( this, "Erro ao criar banco de dados", Toast.LENGTH_LONG ).show();
			this.finish();
		}

		if ( savedInstanceState == null ) {
			addMail();
			addPhone();
		} else {
			Data d = (Data) savedInstanceState.get( "mails" );
			mails = (List<Mail>) d.getData();
			d = (Data) savedInstanceState.get( "phones" );
			phones = (List<Phone>) d.getData();
		}

		listMails = (ListView) findViewById( R.id.listMails );
		listMails.setAdapter( new ItemEmailFormAdapter( this, this.mails ) );
		AndroidUtils.setListViewHeightBasedOnChildren( listMails );

		listPhones = (ListView) findViewById( R.id.listPhones );
		listPhones.setAdapter( new ItemPhoneFormAdapter( this, this.phones ) );
		AndroidUtils.setListViewHeightBasedOnChildren( listPhones );

		Button btn = (Button) findViewById( R.id.btnAddMail );
		btn.setOnClickListener( btnAddMailListener );

		btn = (Button) findViewById( R.id.btnAddPhone );
		btn.setOnClickListener( btnAddPhoneListener );

		btn = (Button) findViewById( R.id.btnSave );
		btn.setOnClickListener( btnSaveListener );

	}

	private void addMail() {
		Mail mail = new Mail();
		mail.setType( TypeMail.OTHER );
		mails.add( mail );
	}

	private void addPhone() {
		Phone phone = new Phone();
		phone.setType( TypePhone.OTHER );
		phones.add( phone );
	}

	private void savePerson() {
		Person person = new Person();
		EditText name = (EditText) findViewById( R.id.editPersonName );
		person.setName( name.getText().toString() );

		try {
			personManager.create( person );
			for ( Mail mail : mails ) {
				mail.setPerson( person );
				mailManager.create( mail );
			}
			for ( Phone phone : phones ) {
				phone.setPerson( person );
				phoneManager.create( phone );
			}
			AndroidUtils.showMessage( this, "Info", "Salvo com sucesso!" );
		} catch ( SQLException e ) {
			e.printStackTrace();
			AndroidUtils.showMessage( this, "Erro", "Erro ao salvar dados!", android.R.drawable.ic_dialog_info );
		}

	}
}
